﻿namespace SFWS.XJSON.Core
{
    using SFWS.XJSON.Core.Properties;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Text;

    public sealed class JsonParser
    {
        public List<string> ParseArray(string json)
        {
            bool flag;
            List<string> list = new List<string>();
            if (string.IsNullOrEmpty(json))
            {
                return list;
            }
            StringReader reader = new StringReader(json);
            JsonContainer container7 = new JsonContainer {
                State = ParseStates.InArray,
                Parent = null
            };
            JsonContainer container = container7;
            JsonContainer parent = container;
            StringBuilder builder = new StringBuilder();
        Label_0334:
            flag = true;
            int num = reader.Read();
            if (num < 0)
            {
                if (parent.Parent != null)
                {
                    throw new ArgumentException(Resources.JsonException, "json");
                }
                if (builder.Length > 0)
                {
                    list.Add(builder.ToString());
                }
                return list;
            }
            char ch = (char) num;
            builder.Append(ch);
            switch (ch)
            {
                case '\\':
                    if (parent.State != ParseStates.InString)
                    {
                        if (parent.State != ParseStates.InStringTranser)
                        {
                            throw new ArgumentException(Resources.JsonException, "json");
                        }
                        parent = parent.Parent;
                    }
                    else
                    {
                        JsonContainer container3 = new JsonContainer {
                            State = ParseStates.InStringTranser,
                            Parent = parent
                        };
                        parent = container3;
                    }
                    goto Label_0334;

                case ',':
                    if (parent.State != ParseStates.InString)
                    {
                        if (parent.State == ParseStates.InStringTranser)
                        {
                            parent = parent.Parent;
                        }
                        else if (parent.Parent == null)
                        {
                            builder.Length--;
                            list.Add(builder.ToString());
                            builder.Length = 0;
                        }
                    }
                    goto Label_0334;

                case '"':
                    if ((parent.State == ParseStates.InString) || (parent.State == ParseStates.InStringTranser))
                    {
                        parent = parent.Parent;
                    }
                    else
                    {
                        JsonContainer container4 = new JsonContainer {
                            State = ParseStates.InString,
                            Parent = parent
                        };
                        parent = container4;
                    }
                    goto Label_0334;

                case '[':
                    if (parent.State != ParseStates.InString)
                    {
                        if (parent.State == ParseStates.InStringTranser)
                        {
                            parent = parent.Parent;
                        }
                        else
                        {
                            JsonContainer container5 = new JsonContainer {
                                State = ParseStates.InArray,
                                Parent = parent
                            };
                            parent = container5;
                        }
                    }
                    goto Label_0334;

                case ']':
                case '}':
                    if (parent.State != ParseStates.InString)
                    {
                        if (parent.State == ParseStates.InStringTranser)
                        {
                            parent = parent.Parent;
                        }
                        else
                        {
                            parent = parent.Parent;
                        }
                    }
                    goto Label_0334;

                case '{':
                    if (parent.State != ParseStates.InString)
                    {
                        if (parent.State == ParseStates.InStringTranser)
                        {
                            parent = parent.Parent;
                        }
                        else
                        {
                            JsonContainer container6 = new JsonContainer {
                                State = ParseStates.InObject,
                                Parent = parent
                            };
                            parent = container6;
                        }
                    }
                    goto Label_0334;
            }
            if (parent.State == ParseStates.InStringTranser)
            {
                parent = parent.Parent;
            }
            goto Label_0334;
        }

        private KeyValuePair<string, string> parseNameValue(string json)
        {
            StringReader reader = new StringReader(json);
            JsonContainer container5 = new JsonContainer {
                State = ParseStates.InObject,
                Parent = null
            };
            JsonContainer container = container5;
            JsonContainer parent = container;
            StringBuilder builder = new StringBuilder();
            bool flag = false;
            while (true)
            {
                int num = reader.Read();
                if (num < 0)
                {
                    throw new ArgumentException(Resources.JsonException, "json");
                }
                char ch = (char) num;
                if (parent.State == ParseStates.InString)
                {
                    builder.Append(ch);
                }
                if (ch == '\\')
                {
                    if (parent.State != ParseStates.InString)
                    {
                        if (parent.State != ParseStates.InStringTranser)
                        {
                            throw new ArgumentException(Resources.JsonException, "json");
                        }
                        parent = parent.Parent;
                    }
                    else
                    {
                        JsonContainer container3 = new JsonContainer {
                            State = ParseStates.InStringTranser,
                            Parent = parent
                        };
                        parent = container3;
                    }
                }
                else if (ch == '"')
                {
                    if (parent.State == ParseStates.InString)
                    {
                        parent = parent.Parent;
                        flag = true;
                    }
                    else if (parent.State == ParseStates.InStringTranser)
                    {
                        parent = parent.Parent;
                    }
                    else
                    {
                        JsonContainer container4 = new JsonContainer {
                            State = ParseStates.InString,
                            Parent = parent
                        };
                        parent = container4;
                    }
                }
                else if (ch == ':')
                {
                    if (parent.State != ParseStates.InString)
                    {
                        if (parent.State != ParseStates.InStringTranser)
                        {
                            if (!flag)
                            {
                                throw new ArgumentException(Resources.JsonException, "json");
                            }
                            builder.Length--;
                            return new KeyValuePair<string, string>(builder.ToString(), reader.ReadToEnd());
                        }
                        parent = parent.Parent;
                    }
                }
                else if (parent.State == ParseStates.InStringTranser)
                {
                    parent = parent.Parent;
                }
            }
        }

        public Dictionary<string, string> ParseObject(string json)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            List<string> list = this.ParseArray(json);
            foreach (string str in list)
            {
                KeyValuePair<string, string> pair = this.parseNameValue(str);
                dictionary.Add(pair.Key, pair.Value);
            }
            return dictionary;
        }

        private class JsonContainer
        {
            public JsonParser.JsonContainer Parent { get; set; }

            public JsonParser.ParseStates State { get; set; }
        }

        private enum ParseStates
        {
            None,
            InString,
            InStringTranser,
            InArray,
            InObject
        }
    }
}

